package cn.tedu.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
public class PasswordEncoderUtil {
    @Bean
    BCryptPasswordEncoder getPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * 加密
     * @param content
     * @return
     */
    public static String enc(String content){

        BCryptPasswordEncoder bCryptPasswordEncoder1  = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder1.encode(content);
    }

    /**
     * 校验  盐值
     * @param content
     * @param encryptContent
     * @return
     */
    public static  boolean decrypt(String content,String encryptContent){
        BCryptPasswordEncoder bCryptPasswordEncoder1 =  new BCryptPasswordEncoder();
        return bCryptPasswordEncoder1.matches(content,encryptContent);
    }

    public static void main(String[] args) {
        // 用户注册
        String enc = enc("123456");
        System.out.println(enc); // 存数据库

        // 用户登录 "123456" 模拟用户输入的密码 enc 理解为数据库查出来的
        boolean decrypt = decrypt("123456", enc);
        System.out.println(decrypt);
    }

}
